/**
 * axios实例的包装方法,返回axios实例,调用和原始axios实例方法相同
 * then通过拦截器,返回接口原始数据(axios返回{config,data,header,request},其中data为原始数据),
 * 拦截会在retCode等于0000才会正常返回,否则会进入catch(reject),把错误信息反馈给用户,使用时一般不用自己处理catch,但要写catch(()=>{})捕获
 * 如需跳过拦截器,可通过设置intercept值
 * @returns axios interceptor instance
 */
import Axios from "axios";
import util from "./util";
import Router from "../router";
import { Message } from "element-ui";
import { Toast } from 'vant';
import md5 from 'js-md5';
let api = process.env.API_ROOT;
let openApi = process.env.OPEN_API_ROOT;
let robotApi = process.env.ROBOT_API_ROOT;
let modelManageApi = process.env.MODEL_MANAGE_ROOT;
let modelManageGetApi = process.env.MODEL_MANAGE_GETAPI_ROOT;
let redirectUrlApi = process.env.REDIRECT_URL;//平台入驻流程v1.7.-扫码获取openid值
let assApi = process.env.ASS_API_URL;//协会备案系统
function getSign (data) {
  var newData = {};
  Object.keys(data).sort().map(key => {
    newData[key] = data[key]
  })
  var result = ""
  for (let name in newData) {
    if (typeof newData[name] != 'function') {
      result += "&" + name + "=" + encodeURI(newData[name]);
    }
  }
  return result.substring(1)
}
// import thisVue from '../mian.js';
// 创建自定义实例默认值
const instance = Axios.create({
  // baseURL: util.apiServer,
  baseURL: api,
  // baseURL: 'http://test_console_api.ihibuilding.cn',
  timeout: 2000000000,
  responseType: "json",
  // withCredentials: true,
  intercept: "all" // 拦截器选项,all:then和catch均通过拦截器,resolve:只有then通过拦截器,reject:只有catch通过拦截器,none:then和catch均不通过拦截器
});
const modelManageInstance = Axios.create({
  // baseURL: util.apiServer,
  baseURL: modelManageApi,
  // baseURL: 'http://test_console_api.ihibuilding.cn',
  // timeout: 1000000,
  responseType: "json",
  // withCredentials: true,
  intercept: "all" // 拦截器选项,all:then和catch均通过拦截器,resolve:只有then通过拦截器,reject:只有catch通过拦截器,none:then和catch均不通过拦截器
});
const modelManageGetApiInstance = Axios.create({
  // baseURL: util.apiServer,
  baseURL: modelManageGetApi,
  // baseURL: 'http://test_console_api.ihibuilding.cn',
  // timeout: 1000000,
  responseType: "json",
  // withCredentials: true,
  intercept: "all" // 拦截器选项,all:then和catch均通过拦截器,resolve:只有then通过拦截器,reject:只有catch通过拦截器,none:then和catch均不通过拦截器
});

const redirectUrlApiInstance = Axios.create({
  // baseURL: util.apiServer,
  baseURL: redirectUrlApi,
  // baseURL: 'http://test_console_api.ihibuilding.cn',
  // timeout: 1000000,
  responseType: "json",
  // withCredentials: true,
  intercept: "all" // 拦截器选项,all:then和catch均通过拦截器,resolve:只有then通过拦截器,reject:只有catch通过拦截器,none:then和catch均不通过拦截器
});
const openInstance = Axios.create({
  baseURL: openApi,
  // timeout: 20000,
  responseType: "json",
  // withCredentials: true,
  intercept: "all"
});
const robotInstance = Axios.create({
  baseURL: robotApi,
  // timeout: 20000,
  responseType: "json",
  // withCredentials: true,
  intercept: "all"
});
const assInstance = Axios.create({
  baseURL: assApi,
  // timeout: 20000,
  responseType: "json",
  // withCredentials: true,
  intercept: "all"
});
robotInstance.interceptors.request.use(config => {
  // axios post请求data值类型为urlencode或URLSearchParams,Content-Type会默认设置为application/x-www-form-urlencoded,但在http请求为参数统一,不允许使用对象类型外值,还是显示设置Content-Type
  if (config.method === "post") {
  }
  // var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlvbklkIjpudWxsLCJwaG9uZSI6bnVsbCwibmlja05hbWUiOiJzdXBlck1hbmFnZXIiLCJvcGVuSWQiOm51bGwsInVzZXJUeXBlIjoyLCJ1c2VyTmFtZSI6bnVsbCwidXNlcklkIjoxLCJ0cyI6MTUyNDIxNjc4Njc5N30.j_c4vgYTi-IIPwzdJD9xLzAQVAiU0bIkiYzeAXULWVc'
  // let token = sessionStorage.getItem("access_token")
  //   ? sessionStorage.getItem("access_token")
  //   : "";
  let token = localStorage.getItem("access_token")
    ? localStorage.getItem("access_token")
    : "";
  let key = sessionStorage.getItem("platformKey")
    ? sessionStorage.getItem("platformKey")
    : "";
  config.headers["key"] = key;
  config.headers["access_token"] = token;
  return config;
});
// 自定义实例添加拦截器
robotInstance.interceptors.response.use(
  res => {
    // console.log('interceptors.response:', res)
    if (res.config.intercept !== "none" && res.config.intercept !== "reject") {
      if (res.status === 200) {
        if (res.data.code === 0) {
          return res.data;
        } else if (res.data.code === 10201 || res.data.code === 10204 || res.data.action === 'Laravel_public_permission_fail') {
          // token失效时跳转到登录页
          sessionStorage.removeItem("token");
          localStorage.removeItem("activeTabsList");
          // location.href = "/login";
          Router.push({
            path: "/login"
          });
        } else if (res.data.code == 33199) {//平台入驻流程v1.7.0-帐号未绑定返回的code值
          return res.data;
        } else {
          // if(Axios.isCancel(res))
          // 如果intercept设置为resolve,then拦截会判断返回码状态,不会弹出错误信息,如果设置为reject,catch拦截只会处理原始错误信息
          if (
            res.config.intercept !== "none" &&
            res.config.intercept !== "resolve"
          ) {
            Message.error(res.data.msg);
          }
          // return Promise.reject(res)
        }
      } else if (res.status === 403) {
        Message.error(res.data.message);
        // location.href = '/error?type=403'
      } else if (res.status === 404) {
        Message.error(res.data.message);
        // location.href = '/error?type=404'
      } else {
        Message.error(res.data.message);
        // location.href = '/error?type=500'
      }
    }

    return res.data;
    // 拦截器中不return返回,then同样会执行
  },
  err => {
    if (err.config.intercept !== "none" && err.config.intercept !== "resolve") {
      // 这里错误拦截只会是err原始对象,不用判断err.response
      // Message.error(
      //   //`${err.response.status}：${err.response.statusText}`
      // )
    }
    // promise reject后必须有catch捕获错误,否则浏览器会报错,想在else中返回reject,就可以只在catch不启用拦截的情况,返回reject(但这样也有弊端,虽然可以不用必须写catch,但拦截器和catch不能同时链式使用),then拦截中不成功其他编码reject没法处理
    return Promise.reject(err);
  }
);

assInstance.interceptors.request.use(config => {
  // axios post请求data值类型为urlencode或URLSearchParams,Content-Type会默认设置为application/x-www-form-urlencoded,但在http请求为参数统一,不允许使用对象类型外值,还是显示设置Content-Type
  if (config.method === 'post') {
  }

  // var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlvbklkIjpudWxsLCJwaG9uZSI6bnVsbCwibmlja05hbWUiOiJzdXBlck1hbmFnZXIiLCJvcGVuSWQiOm51bGwsInVzZXJUeXBlIjoyLCJ1c2VyTmFtZSI6bnVsbCwidXNlcklkIjoxLCJ0cyI6MTUyNDIxNjc4Njc5N30.j_c4vgYTi-IIPwzdJD9xLzAQVAiU0bIkiYzeAXULWVc'
  var token = sessionStorage.getItem('access_token')
    ? sessionStorage.getItem('access_token')
    : ''
  let key = sessionStorage.getItem('platformKey')
    ? sessionStorage.getItem('platformKey')
    : ''
  config.headers['key'] = key
  config.headers['access_token'] = token
  return config
})

// 自定义实例添加拦截器
assInstance.interceptors.response.use(
  res => {
    // console.log('interceptors.response:', res)
    if (res.config.intercept !== 'none' && res.config.intercept !== 'reject') {
      if (res.status === 200) {
        if (res.data && res.data.code === 0) {
          return res.data
        } else if (
          res.data &&
          res.data.action === 'balance-server_public_permission_fail'
        ) {
          // token失效时跳转到登录页
          sessionStorage.removeItem('access_token')
          localStorage.removeItem('activeTabsList')
          // location.href = '/login'
          Router.push({
            path: '/login'
          })
        } else {
          // if(Axios.isCancel(res))
          // 如果intercept设置为resolve,then拦截会判断返回码状态,不会弹出错误信息,如果设置为reject,catch拦截只会处理原始错误信息
          if (
            res.config.intercept !== 'none' &&
            res.config.intercept !== 'resolve'
          ) {
            if (
              Object.prototype.toString.call(res.data) === '[object Object]'
            ) {
              // 当返回的msg是未知错误时，根据action手动设置报错信息
              Message.error(res.data.msg)
            }
          }
          return res.data
          // return Promise.reject(res)
        }
      } else if (res.status === 403) {
        location.href = '/error?type=403'
      } else if (res.status === 404) {
        location.href = '/error?type=404'
      } else if (res.status === 422) {
        console.log(res.status, '422')
      } else {
        location.href = '/error?type=500'
      }
    }
    return res.data
    // 拦截器中不return返回,then同样会执行
  },
  err => {
    console.log('interceptors.response:err:', err.response)
    // console.log(err.response)
    if (err.response.config.intercept !== 'none' && err.response.config.intercept !== 'resolve') {
      // 这里错误拦截只会是err原始对象,不用判断err.response
      // if (err.response.status === 403) {
      //   location.href = '/error?type=403'
      // } else if (err.response.status === 404) {
      //   location.href = '/error?type=404'
      // } else if (err.response.status === 500) {
      //   location.href = '/error?type=500'
      // } else {

      // }
      // 返回的报错信息可能有多个，显示第一个
      let keys = Object.keys(err.response.data)
      let errMsgList = err.response.data['msg'] || '服务器内部错误'
      Message.error(
        `${errMsgList}`
      )
    }
    // promise reject后必须有catch捕获错误,否则浏览器会报错,想在else中返回reject,就可以只在catch不启用拦截的情况,返回reject(但这样也有弊端,虽然可以不用必须写catch,但拦截器和catch不能同时链式使用),then拦截中不成功其他编码reject没法处理
    return Promise.reject(err)
  }
)



instance.interceptors.request.use(config => {
  // axios post请求data值类型为urlencode或URLSearchParams,Content-Type会默认设置为application/x-www-form-urlencoded,但在http请求为参数统一,不允许使用对象类型外值,还是显示设置Content-Type
  if (config.method === "post") {
  }

  // var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlvbklkIjpudWxsLCJwaG9uZSI6bnVsbCwibmlja05hbWUiOiJzdXBlck1hbmFnZXIiLCJvcGVuSWQiOm51bGwsInVzZXJUeXBlIjoyLCJ1c2VyTmFtZSI6bnVsbCwidXNlcklkIjoxLCJ0cyI6MTUyNDIxNjc4Njc5N30.j_c4vgYTi-IIPwzdJD9xLzAQVAiU0bIkiYzeAXULWVc'
  // var token = sessionStorage.getItem("access_token")
  //   ? sessionStorage.getItem("access_token")
  //   : "";
  let requestData = {
    timeStamp: new Date().getTime(),
    randomNum: (Math.random() * 100000).toFixed(0)
  }
  // 接口添加sign加密
  if (config.method === 'get') {
    config.params = config.params || {}
    config.params.timeStamp = requestData.timeStamp
    config.params.randomNum = requestData.randomNum
  } else {
    config.data = config.data || {}
    config.data.timeStamp = requestData.timeStamp
    config.data.randomNum = requestData.randomNum
  }
  let sign = getSign(requestData)
  let signMd5 = md5(md5("#1scv6zfzSR1wLaWN") + md5(sign));
  config.headers['sign'] = 'bearer1:' + signMd5
  var token = localStorage.getItem("access_token")
    ? localStorage.getItem("access_token")
    : "";
  let key = sessionStorage.getItem("platformKey")
    ? sessionStorage.getItem("platformKey")
    : "";
  config.headers["key"] = key;
  config.headers["access_token"] = token;
  return config;
});

// 自定义实例添加拦截器
instance.interceptors.response.use(
  res => {
    // console.log("interceptors.response:", res);
    if (res.config.intercept !== "none" && res.config.intercept !== "reject") {
      if (res.status === 200) {
        if (res.data && res.data.code === 0) {
          return res.data;
        } else if (
          res.data &&
          (res.data.code === 10201 || res.data.code === 10204 || res.data.action === 'Laravel_public_permission_fail')
        ) {
          // token失效时跳转到登录页
          sessionStorage.removeItem("token");
          localStorage.removeItem("activeTabsList");
          // location.href = "/login";
          Router.push({
            path: "/login"
          });
        } else {
          // if(Axios.isCancel(res))
          // 如果intercept设置为resolve,then拦截会判断返回码状态,不会弹出错误信息,如果设置为reject,catch拦截只会处理原始错误信息
          if (
            res.config.intercept !== "none" &&
            res.config.intercept !== "resolve"
          ) {
            if (
              Object.prototype.toString.call(res.data) === "[object Object]"
            ) {
              if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
                Toast({
                  message: res.data.msg,
                  position: 'top'
                });
              } else {
                Message.error(res.data.msg);
              }
            }
          }
          return res.data;
          // return Promise.reject(res)
        }
      } else if (res.status === 403) {
        Message.error(res.data.message);
        // location.href = '/error?type=403'
      } else if (res.status === 404) {
        Message.error(res.data.message);
        // location.href = '/error?type=404'
      } else {
        Message.error(res.data.message);
        // location.href = '/error?type=500'
      }
    }

    return res.data;
    // 拦截器中不return返回,then同样会执行
  },
  err => {
    console.log("interceptors.response:err:", err);
    if (err.config && err.config.intercept !== "none" && err.config.intercept !== "resolve") {
      // 这里错误拦截只会是err原始对象,不用判断err.response
      // Message.error(
      //   //`${err.response.status}：${err.response.statusText}`
      // )
    }
    // promise reject后必须有catch捕获错误,否则浏览器会报错,想在else中返回reject,就可以只在catch不启用拦截的情况,返回reject(但这样也有弊端,虽然可以不用必须写catch,但拦截器和catch不能同时链式使用),then拦截中不成功其他编码reject没法处理
    return Promise.reject(err);
  }
);

openInstance.interceptors.request.use(config => {
  // axios post请求data值类型为urlencode或URLSearchParams,Content-Type会默认设置为application/x-www-form-urlencoded,但在http请求为参数统一,不允许使用对象类型外值,还是显示设置Content-Type
  if (config.method === "post") {
  }
  // var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlvbklkIjpudWxsLCJwaG9uZSI6bnVsbCwibmlja05hbWUiOiJzdXBlck1hbmFnZXIiLCJvcGVuSWQiOm51bGwsInVzZXJUeXBlIjoyLCJ1c2VyTmFtZSI6bnVsbCwidXNlcklkIjoxLCJ0cyI6MTUyNDIxNjc4Njc5N30.j_c4vgYTi-IIPwzdJD9xLzAQVAiU0bIkiYzeAXULWVc'
  // let token = sessionStorage.getItem("access_token")
  //   ? sessionStorage.getItem("access_token")
  //   : "";
  let requestData = {
    timeStamp: new Date().getTime(),
    randomNum: (Math.random() * 100000).toFixed(0)
  }
  // 接口添加sign加密
  if (config.method === 'get') {
    config.params = config.params || {}
    config.params.timeStamp = requestData.timeStamp
    config.params.randomNum = requestData.randomNum
  } else {
    config.data = config.data || {}
    config.data.timeStamp = requestData.timeStamp
    config.data.randomNum = requestData.randomNum
  }
  let sign = getSign(requestData)
  let signMd5 = md5(md5("#1scv6zfzSR1wLaWN") + md5(sign));
  config.headers['sign'] = 'bearer1:' + signMd5
  let token = localStorage.getItem("access_token")
    ? localStorage.getItem("access_token")
    : "";
  let key = sessionStorage.getItem("platformKey")
    ? sessionStorage.getItem("platformKey")
    : "";
  config.headers["key"] = key;
  config.headers["access_token"] = token;
  return config;
});
// 自定义实例添加拦截器
openInstance.interceptors.response.use(
  res => {
    // console.log('interceptors.response:', res)
    if (res.config.intercept !== "none" && res.config.intercept !== "reject") {
      if (res.status === 200) {
        if (res.data.code === 0) {
          return res.data;
        } else if (res.data.code === 10201 || res.data.code === 10204 || res.data.action === 'Laravel_public_permission_fail') {
          // token失效时跳转到登录页
          sessionStorage.removeItem("token");
          localStorage.removeItem("activeTabsList");
          // location.href = "/login";
          Router.push({
            path: "/login"
          });
        } else if (res.data.code == 33199) {//平台入驻流程v1.7.0-帐号未绑定返回的code值
          return res.data;
        } else {
          // if(Axios.isCancel(res))
          // 如果intercept设置为resolve,then拦截会判断返回码状态,不会弹出错误信息,如果设置为reject,catch拦截只会处理原始错误信息
          if (
            res.config.intercept !== "none" &&
            res.config.intercept !== "resolve"
          ) {
            Message.error(res.data.msg);
          }
          // return Promise.reject(res)
        }
      } else if (res.status === 403) {
        Message.error(res.data.message);
        // location.href = '/error?type=403'
      } else if (res.status === 404) {
        Message.error(res.data.message);
        // location.href = '/error?type=404'
      } else {
        Message.error(res.data.message);
        // location.href = '/error?type=500'
      }
    }

    return res.data;
    // 拦截器中不return返回,then同样会执行
  },
  err => {
    if (err.config.intercept !== "none" && err.config.intercept !== "resolve") {
      // 这里错误拦截只会是err原始对象,不用判断err.response
      // Message.error(
      //   //`${err.response.status}：${err.response.statusText}`
      // )
    }
    // promise reject后必须有catch捕获错误,否则浏览器会报错,想在else中返回reject,就可以只在catch不启用拦截的情况,返回reject(但这样也有弊端,虽然可以不用必须写catch,但拦截器和catch不能同时链式使用),then拦截中不成功其他编码reject没法处理
    return Promise.reject(err);
  }
);

//处定义实例添加拦截器
modelManageInstance.interceptors.response.use(
  res => {
    if (res.config.intercept !== "none" && res.config.intercept !== "reject") {
      if (res.status === 200) {
        if (res.data && res.data.httpCode === 200) {
          return res.data;
        } else if (
          res.data &&
          (res.data.code === 10201 || res.data.code === 10204 || res.data.action === 'Laravel_public_permission_fail')//res.data.httpCode === 10201 || res.data.httpCode === 10204 || res.data.action === 'Laravel_public_permission_fail'
        ) {
          // token失效时跳转到登录页
          sessionStorage.removeItem("token");//MacroAuth从sessionStorage删除
          localStorage.removeItem("activeTabsList");
          Router.push({
            path: "/login"
          });
        } else {
          // if(Axios.isCancel(res))
          // 如果intercept设置为resolve,then拦截会判断返回码状态,不会弹出错误信息,如果设置为reject,catch拦截只会处理原始错误信息
          if (
            res.config.intercept !== "none" &&
            res.config.intercept !== "resolve"
          ) {
            if (
              Object.prototype.toString.call(res.data) === "[object Object]"
            ) {
              Message.error(res.data.sys_msg);
            }
          }
          return res.data;
          // return Promise.reject(res)
        }
      } else if (res.status === 403) {
        Message.error(res.data.sys_msg);
        // location.href = '/error?type=403'
      } else if (res.status === 404) {
        Message.error(res.data.sys_msg);
        // location.href = '/error?type=404'
      } else {
        Message.error(res.data.sys_msg);
        // location.href = '/error?type=500'
      }
    }

    return res.data;
    // 拦截器中不return返回,then同样会执行
  },
  err => {
    console.log("interceptors.response:err:", err);
    if (err.config.intercept !== "none" && err.config.intercept !== "resolve") {
      // 这里错误拦截只会是err原始对象,不用判断err.response
      // Message.error(
      //   //`${err.response.status}：${err.response.statusText}`
      // )
    }
    // promise reject后必须有catch捕获错误,否则浏览器会报错,想在else中返回reject,就可以只在catch不启用拦截的情况,返回reject(但这样也有弊端,虽然可以不用必须写catch,但拦截器和catch不能同时链式使用),then拦截中不成功其他编码reject没法处理
    return Promise.reject(err);
  }
);
modelManageInstance.interceptors.request.use(config => {
  // axios post请求data值类型为urlencode或URLSearchParams,Content-Type会默认设置为application/x-www-form-urlencoded,但在http请求为参数统一,不允许使用对象类型外值,还是显示设置Content-Type
  if (config.method === "post") {
  }

  // var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlvbklkIjpudWxsLCJwaG9uZSI6bnVsbCwibmlja05hbWUiOiJzdXBlck1hbmFnZXIiLCJvcGVuSWQiOm51bGwsInVzZXJUeXBlIjoyLCJ1c2VyTmFtZSI6bnVsbCwidXNlcklkIjoxLCJ0cyI6MTUyNDIxNjc4Njc5N30.j_c4vgYTi-IIPwzdJD9xLzAQVAiU0bIkiYzeAXULWVc'
  var token = sessionStorage.getItem("MacroAuth")
    ? sessionStorage.getItem("MacroAuth")
    : "";
  let key = sessionStorage.getItem("TenantId")
    ? sessionStorage.getItem("TenantId")
    : "";
  config.headers["Macro-Auth"] = token;
  config.headers["Authorization"] = "Basic empqZzp6aGVqaWFuZ2ppYW5nb25nMjAyMQ=="
  return config;
});

//处定义实例添加拦截器
modelManageGetApiInstance.interceptors.response.use(
  res => {
    // console.log('interceptors.response:', res)
    if (res.config.intercept !== "none" && res.config.intercept !== "reject") {
      if (res.status === 200) {
        if (res.data.code === 0) {
          return res.data;
        } else if (res.data.code === 10201 || res.data.code === 10204 || res.data.action === 'Laravel_public_permission_fail') {
          // token失效时跳转到登录页
          sessionStorage.removeItem("token");
          localStorage.removeItem("activeTabsList");
          // location.href = "/login";
          Router.push({
            path: "/login"
          });
        } else {
          // if(Axios.isCancel(res))
          // 如果intercept设置为resolve,then拦截会判断返回码状态,不会弹出错误信息,如果设置为reject,catch拦截只会处理原始错误信息
          if (
            res.config.intercept !== "none" &&
            res.config.intercept !== "resolve"
          ) {
            Message.error(res.data.msg);
          }
          // return Promise.reject(res)
        }
      } else if (res.status === 403) {
        Message.error(res.data.message);
        // location.href = '/error?type=403'
      } else if (res.status === 404) {
        Message.error(res.data.message);
        // location.href = '/error?type=404'
      } else {
        Message.error(res.data.message);
        // location.href = '/error?type=500'
      }
    }

    return res.data;
    // 拦截器中不return返回,then同样会执行
  },
  err => {
    if (err.config.intercept !== "none" && err.config.intercept !== "resolve") {
      // 这里错误拦截只会是err原始对象,不用判断err.response
      // Message.error(
      //   //`${err.response.status}：${err.response.statusText}`
      // )
    }
    // promise reject后必须有catch捕获错误,否则浏览器会报错,想在else中返回reject,就可以只在catch不启用拦截的情况,返回reject(但这样也有弊端,虽然可以不用必须写catch,但拦截器和catch不能同时链式使用),then拦截中不成功其他编码reject没法处理
    return Promise.reject(err);
  }
);
modelManageGetApiInstance.interceptors.request.use(config => {
  // axios post请求data值类型为urlencode或URLSearchParams,Content-Type会默认设置为application/x-www-form-urlencoded,但在http请求为参数统一,不允许使用对象类型外值,还是显示设置Content-Type
  if (config.method === "post") {
  }
  // var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlvbklkIjpudWxsLCJwaG9uZSI6bnVsbCwibmlja05hbWUiOiJzdXBlck1hbmFnZXIiLCJvcGVuSWQiOm51bGwsInVzZXJUeXBlIjoyLCJ1c2VyTmFtZSI6bnVsbCwidXNlcklkIjoxLCJ0cyI6MTUyNDIxNjc4Njc5N30.j_c4vgYTi-IIPwzdJD9xLzAQVAiU0bIkiYzeAXULWVc'
  // let token = sessionStorage.getItem("access_token")
  //   ? sessionStorage.getItem("access_token")
  //   : "";
  let token = localStorage.getItem("access_token")
    ? localStorage.getItem("access_token")
    : "";
  let key = sessionStorage.getItem("platformKey")
    ? sessionStorage.getItem("platformKey")
    : "";
  config.headers["key"] = key;
  config.headers["access_token"] = token;
  return config;
});
//redirectUrlApiInstance
//处定义实例添加拦截器
redirectUrlApiInstance.interceptors.response.use(
  res => {
    // console.log('interceptors.response:', res)
    if (res.config.intercept !== "none" && res.config.intercept !== "reject") {
      if (res.status === 200) {
        if (res.data.code === 0) {
          return res.data;
        } else if (res.data.code === 10201 || res.data.code === 10204 || res.data.action === 'Laravel_public_permission_fail') {
          // token失效时跳转到登录页
          sessionStorage.removeItem("token");
          localStorage.removeItem("activeTabsList");
          // location.href = "/login";
          Router.push({
            path: "/login"
          });
        } else if (res.data.code == 33199) {//平台入驻流程v1.7.0-帐号未绑定返回的code值
          return res.data;
        } else {
          // if(Axios.isCancel(res))
          // 如果intercept设置为resolve,then拦截会判断返回码状态,不会弹出错误信息,如果设置为reject,catch拦截只会处理原始错误信息
          if (
            res.config.intercept !== "none" &&
            res.config.intercept !== "resolve"
          ) {
            Message.error(res.data.msg);
          }
          // return Promise.reject(res)
        }
      } else if (res.status === 403) {
        Message.error(res.data.message);
        // location.href = '/error?type=403'
      } else if (res.status === 404) {
        Message.error(res.data.message);
        // location.href = '/error?type=404'
      } else {
        Message.error(res.data.message);
        // location.href = '/error?type=500'
      }
    }
    return res.data;
    // 拦截器中不return返回,then同样会执行
  },
  err => {
    if (err.config.intercept !== "none" && err.config.intercept !== "resolve") {
      // 这里错误拦截只会是err原始对象,不用判断err.response
      // Message.error(
      //   //`${err.response.status}：${err.response.statusText}`
      // )
    }
    // promise reject后必须有catch捕获错误,否则浏览器会报错,想在else中返回reject,就可以只在catch不启用拦截的情况,返回reject(但这样也有弊端,虽然可以不用必须写catch,但拦截器和catch不能同时链式使用),then拦截中不成功其他编码reject没法处理
    return Promise.reject(err);
  }
);
redirectUrlApiInstance.interceptors.request.use(config => {
  // axios post请求data值类型为urlencode或URLSearchParams,Content-Type会默认设置为application/x-www-form-urlencoded,但在http请求为参数统一,不允许使用对象类型外值,还是显示设置Content-Type
  if (config.method === "post") {
  }
  // var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlvbklkIjpudWxsLCJwaG9uZSI6bnVsbCwibmlja05hbWUiOiJzdXBlck1hbmFnZXIiLCJvcGVuSWQiOm51bGwsInVzZXJUeXBlIjoyLCJ1c2VyTmFtZSI6bnVsbCwidXNlcklkIjoxLCJ0cyI6MTUyNDIxNjc4Njc5N30.j_c4vgYTi-IIPwzdJD9xLzAQVAiU0bIkiYzeAXULWVc'
  // let token = sessionStorage.getItem("access_token")
  //   ? sessionStorage.getItem("access_token")
  //   : "";
  let token = localStorage.getItem("access_token")
    ? localStorage.getItem("access_token")
    : "";
  let key = sessionStorage.getItem("platformKey")
    ? sessionStorage.getItem("platformKey")
    : "";
  config.headers["key"] = key;
  config.headers["access_token"] = token;
  return config;
});
export { instance, openInstance, robotInstance, modelManageInstance, modelManageGetApiInstance, redirectUrlApiInstance, assInstance };
